//
//  SISSReserveCommissionedNotaryViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2018/10/24.
//  Copyright © 2018年 优房网. All rights reserved.
//

#import "SISSReserveCommissionedNotaryViewController.h"
#import "SISSTitleView.h"
#import "EBDropdownListView.h"
#import "SISSTextView.h"
#import "SISSReserveCommissionedNotaryShowViewController.h"
#import "SISSBaseWebViewController.h"
#import "SISSDatePickerView.h"
#import "SISSNetworkingManager+HouseProgress.h"

@interface SISSReserveCommissionedNotaryViewController ()<SISSDatePickerViewDelegate>

/// 预约信息
@property (weak, nonatomic) SISSTitleView *reserveTitleView;
@property (weak, nonatomic) UIButton *topButton;
@property (weak, nonatomic) UILabel *nameTitleLabel;
@property (weak, nonatomic) UITextField *nameTextFeild;
@property (weak, nonatomic) UILabel *reserveTimeTitleLabel;
@property (weak, nonatomic) UIButton *timeChooseButton;
@property (weak, nonatomic) EBDropdownListView *timeDropdownListView;
@property (weak, nonatomic) UILabel *addressTitleLabel;
@property (weak, nonatomic) SISSTextView *addressTextView;
/// 所需资料
@property (weak, nonatomic) SISSTitleView *requiredInfoTitleView;
@property (weak, nonatomic) SISSTextView *requiredInfoTextView;
/// 注意事项
@property (weak, nonatomic) SISSTitleView *noticeTitleView;
@property (weak, nonatomic) SISSTextView *noticeTextView;

/// 确定
@property (weak, nonatomic) UIButton *confirmButton;

@property (copy, nonatomic) NSString *dateStr;
@property (copy, nonatomic) NSString *timeQuantum;
@property (strong, nonatomic) NSDate *date;



@end

@implementation SISSReserveCommissionedNotaryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"登记公证委托信息";
    
    self.date = [NSDate new];
    
    if (self.notaryModel) {
        self.nameTextFeild.text = self.notaryModel.name;
        if (self.notaryModel.applyTime.length > 10) {
            self.dateStr = [self.notaryModel.applyTime substringToIndex:10];
            self.timeQuantum = [[self.notaryModel.applyTime substringFromIndex:10] stringByReplacingOccurrencesOfString:@" " withString:@""];
            [self.timeChooseButton setTitle:self.dateStr forState:UIControlStateNormal];
            [self.timeChooseButton setTitleColor:MAIN_TEXT_COLOR forState:UIControlStateNormal];
            
            [self.timeChooseButton setTitleColor:MAIN_TEXT_COLOR forState:UIControlStateNormal];
            for (NSInteger i = 0; i < self.timeDropdownListView.dataSource.count; i++) {
                EBDropdownListItem *item = self.timeDropdownListView.dataSource[i];
                if ([self.timeQuantum isEqualToString:item.itemName]) {
                    self.timeDropdownListView.selectedIndex = [item.itemId integerValue] - 1;
                }
            }
        }
        
        self.addressTextView.text = self.notaryModel.address;
        self.requiredInfoTextView.text = self.notaryModel.file;
        self.noticeTextView.text = self.notaryModel.attention;
    }
}

- (void)setModel {
    EBDropdownListItem *item1 = [[EBDropdownListItem alloc] initWithItem:@"1" itemName:@"9:00-10:00"];
    EBDropdownListItem *item2 = [[EBDropdownListItem alloc] initWithItem:@"2" itemName:@"10:00-11:00"];
    EBDropdownListItem *item3 = [[EBDropdownListItem alloc] initWithItem:@"3" itemName:@"11:00-12:00"];
    EBDropdownListItem *item4 = [[EBDropdownListItem alloc] initWithItem:@"4" itemName:@"14:00-15:00"];
    EBDropdownListItem *item5 = [[EBDropdownListItem alloc] initWithItem:@"6" itemName:@"15:00-16:00"];
    EBDropdownListItem *item6 = [[EBDropdownListItem alloc] initWithItem:@"6" itemName:@"16:00-17:00"];
    
    self.timeDropdownListView.dataSource = @[item1, item2, item3, item4, item5, item6];
    self.timeQuantum = item1.itemName;
}

#pragma mark - SISSDatePickerViewDelegate
- (void)determine:(SISSDatePickerView *)choose date:(NSDate *)date {
    
    NSString *timeStr = [choose stringFromDate:date];
    NSLog(@"determine  %@",timeStr);
//    self.timeView.rightStr = timeStr;
    [self.timeChooseButton setTitle:timeStr forState:UIControlStateNormal];
    [self.timeChooseButton setTitleColor:MAIN_TEXT_COLOR forState:UIControlStateNormal];
    self.dateStr = timeStr;
    self.date = date;
}

- (void)changeTime:(SISSDatePickerView *)choose date:(NSDate *)date {
    
    NSString *timeStr = [choose stringFromDate:date];
    NSLog(@"changeTime  %@",timeStr);
}

- (void)confirmButtonClickAction {
    NSLog(@"确定按钮点击事件");
    NSString *name = self.nameTextFeild.text.length > 0 ? self.nameTextFeild.text : @"";
    NSString *applyTime = [NSString stringWithFormat:@"%@ %@",self.dateStr,self.timeQuantum];
    NSString *address = self.addressTextView.text.length > 0 ? self.addressTextView.text : @"";
    NSString *file = self.requiredInfoTextView.text.length > 0 ? self.requiredInfoTextView.text : @"";
    NSString *attention = self.noticeTextView.text.length > 0 ? self.noticeTextView.text : @"";
    if (name.length == 0) {
        [SISSTools showSVErrorTitle:@"请输入公证处办事处名称" andTime:2 andColor:YES];
        return;
    }
    if (self.dateStr.length == 0) {
        [SISSTools showSVErrorTitle:@"请选择预约时间" andTime:2 andColor:YES];
        return;
    }
    if (address.length == 0) {
        [SISSTools showSVErrorTitle:@"请输入公证处办事处地址" andTime:2 andColor:YES];
        return;
    }
    if (file.length == 0) {
        [SISSTools showSVErrorTitle:@"请输入业主所需资料" andTime:2 andColor:YES];
        return;
    }
    
    if (self.notaryModel) {
        NSDictionary *paramters = @{
                                    @"id": self.notaryModel.ID,
                                    @"spId": [SISSUserAccount shared].spId,
                                    @"ahbpId": self.ahbpId,
                                    @"name": name,
                                    @"applyTime": applyTime,
                                    @"address": address,
                                    @"file": file,
                                    @"attention": attention
                                    };
        [kNetworkManager updateNotaryInfoWithParamters:paramters success:^(id responseObject) {
            if ([responseObject[@"code"] integerValue] == 200) {
                SISSReserveCommissionedNotaryShowViewController *vc = [[SISSReserveCommissionedNotaryShowViewController alloc] init];
                
                vc.ahbpId = self.ahbpId;
                [self.navigationController pushViewController:vc animated:YES];
            }
        } failed:^(NSError *error) {
            NSLog(@"%@",error);
        }];
    } else {
        NSDictionary *paramters = @{
                                    @"spId": [SISSUserAccount shared].spId,
                                    @"ahbpId": self.ahbpId,
                                    @"name": name,
                                    @"applyTime": applyTime,
                                    @"address": address,
                                    @"file": file,
                                    @"attention": attention
                                    };
        [kNetworkManager insertNotaryInfoWithParamters:paramters success:^(id responseObject) {
            if ([responseObject[@"code"] integerValue] == 200) {
                SISSReserveCommissionedNotaryShowViewController *vc = [[SISSReserveCommissionedNotaryShowViewController alloc] init];
                
                vc.ahbpId = self.ahbpId;
                [self.navigationController pushViewController:vc animated:YES];
            }
        } failed:^(NSError *error) {
            NSLog(@"%@",error);
        }];
    }
}

- (void)topButtonClickAction {
    SISSBaseWebViewController *vc = [[SISSBaseWebViewController alloc] init];
    vc.url = @"http://www.szgzc.net.cn/";
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)timeChooseButtonClickAction {
    [self.view endEditing:YES];
    SISSDatePickerView *datePickerView = [[SISSDatePickerView alloc] initWithType:UIDatePickerModeDate title:@"预约日期"];
    [datePickerView setNowTime:self.date];
    datePickerView.delegate = self;
}

#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    SISSTitleView *reserveTitleView = [[SISSTitleView alloc] init];
    reserveTitleView.titleStr = @"预约信息";
    [self.containerView addSubview:reserveTitleView];
    self.reserveTitleView = reserveTitleView;
    
    UIButton *topButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [topButton setTitle:@"未预约？" forState:UIControlStateNormal];
    topButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [topButton setTitleColor:COLOR(0x1B2BFF) forState:UIControlStateNormal];
    [topButton addTarget:self action:@selector(topButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    [reserveTitleView addSubview:topButton];
    self.topButton = topButton;
    
    UILabel *nameTitleLabel = [UILabel yyr_labelWithText:@"公证处办事处：" fontSize:15 textColor:MAIN_TEXT_COLOR];
    [self.containerView addSubview:nameTitleLabel];
    self.nameTitleLabel = nameTitleLabel;
    
    UITextField *nameTextFeild = [[UITextField alloc] init];
    nameTextFeild.placeholder = @"请您输入办事处名称";
    nameTextFeild.textColor = MAIN_TEXT_COLOR;
    nameTextFeild.layer.borderColor = SECONDARY_TEXT_COLOR.CGColor;
    nameTextFeild.layer.borderWidth = 1;
    [self.containerView addSubview:nameTextFeild];
    self.nameTextFeild = nameTextFeild;
    
    UILabel *reserveTimeTitleLabel = [UILabel yyr_labelWithText:@"预约时间段：" fontSize:15 textColor:MAIN_TEXT_COLOR];
    [self.containerView addSubview:reserveTimeTitleLabel];
    self.reserveTimeTitleLabel = reserveTimeTitleLabel;
    
    UIButton *timeChooseButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [timeChooseButton setTitle:@"选择年月日" forState:UIControlStateNormal];
    [timeChooseButton setTitleColor:SECONDARY_TEXT_COLOR forState:UIControlStateNormal];
    timeChooseButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [timeChooseButton.layer setBorderWidth:1];
    [timeChooseButton.layer setBorderColor:SECONDARY_TEXT_COLOR.CGColor];
    [timeChooseButton addTarget:self action:@selector(timeChooseButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    [self.containerView addSubview:timeChooseButton];
    self.timeChooseButton = timeChooseButton;
    
    EBDropdownListView *timeDropdownListView = [[EBDropdownListView alloc] init];
    timeDropdownListView.selectedIndex = 0;
    [timeDropdownListView setViewBorder:0.5 borderColor:[UIColor grayColor] cornerRadius:2];
    [self.containerView addSubview:timeDropdownListView];
    self.timeDropdownListView = timeDropdownListView;
    [self.timeDropdownListView setDropdownListViewSelectedBlock:^(EBDropdownListView * _Nonnull dropdownListView) {
        NSString *msgString = [NSString stringWithFormat:
                               @"selected name:%@  id:%@  index:%ld"
                               , dropdownListView.selectedItem.itemName
                               , dropdownListView.selectedItem.itemId
                               , dropdownListView.selectedIndex];
        self.timeQuantum = dropdownListView.selectedItem.itemName;
        NSLog(@"%@",msgString);
    }];
    
    UILabel *addressTitleLabel = [UILabel yyr_labelWithText:@"公证处办事处地址：" fontSize:15 textColor:MAIN_TEXT_COLOR];
    [self.containerView addSubview:addressTitleLabel];
    self.addressTitleLabel = addressTitleLabel;
    
    SISSTextView *addressTextView = [[SISSTextView alloc] init];
    addressTextView.placeholder = @"公证处办事处地址";
    addressTextView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.containerView addSubview:addressTextView];
    self.addressTextView = addressTextView;
    
    SISSTitleView *requiredInfoTitleView = [[SISSTitleView alloc] init];
    requiredInfoTitleView.titleStr = @"业主所需资料";
    [self.containerView addSubview:requiredInfoTitleView];
    self.requiredInfoTitleView = requiredInfoTitleView;
    
    SISSTextView *requiredInfoTextView = [[SISSTextView alloc] init];
    requiredInfoTextView.placeholder = @"身份证原件、房产证复印件、借款抵押合同原件、户口薄原件、社保卡或驾驶证原件";
    requiredInfoTextView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.containerView addSubview:requiredInfoTextView];
    self.requiredInfoTextView = requiredInfoTextView;
    
    SISSTitleView *noticeTitleView = [[SISSTitleView alloc] init];
    noticeTitleView.titleStr = @"注意事项";
    [self.containerView addSubview:noticeTitleView];
    self.noticeTitleView = noticeTitleView;
    
    SISSTextView *noticeTextView = [[SISSTextView alloc] init];
    noticeTextView.placeholder = @"身份证原件、房产证复印件、借款抵押合同原件、户口薄原件、社保卡或驾驶证原件";
    noticeTextView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.containerView addSubview:noticeTextView];
    self.noticeTextView = noticeTextView;
    
    UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
    [confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [confirmButton setBackgroundColor:MAIN_COLOR];
    [confirmButton addTarget:self action:@selector(confirmButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    confirmButton.layer.cornerRadius = 4;
    confirmButton.layer.masksToBounds = YES;
    [self.containerView addSubview:confirmButton];
    self.confirmButton = confirmButton;
}

- (void)initConstraint {
    [super initConstraint];
    
    WeakSelf;
    CGSize buttonSize = CGSizeMake(SCREEN_WIDTH * 0.8, 40);
    [self.reserveTitleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.containerView);
        make.left.right.equalTo(weakSelf.containerView);
        make.height.mas_equalTo(5 * globelMargin);
    }];
    
    [self.topButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.reserveTitleView.mas_right).offset(- 3 * globelMargin);
        make.centerY.equalTo(weakSelf.reserveTitleView);
    }];
    
    [self.nameTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.reserveTitleView.mas_bottom).offset(2 * globelMargin);
        make.left.equalTo(weakSelf.containerView).offset(2 * globelMargin);
    }];
    
    [self.nameTextFeild mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.nameTitleLabel);
        make.left.equalTo(weakSelf.nameTitleLabel.mas_right).offset(globelMargin);
//        make.right.equalTo(weakSelf.containerView).offset(-2 * globelMargin);
        make.height.mas_equalTo(3 * globelMargin);
    }];
    
    [self.reserveTimeTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.nameTitleLabel.mas_bottom).offset(2 * globelMargin);
        make.left.equalTo(weakSelf.nameTitleLabel);
    }];
    
    [self.timeChooseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.reserveTimeTitleLabel);
        make.left.equalTo(weakSelf.reserveTimeTitleLabel.mas_right).offset(globelMargin);
        make.width.mas_equalTo(14 * globelMargin);
        make.height.mas_equalTo(3 * globelMargin);
    }];
    
    [self.timeDropdownListView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.reserveTimeTitleLabel);
        make.left.equalTo(weakSelf.timeChooseButton.mas_right).offset(globelMargin);
        make.right.equalTo(weakSelf.containerView).offset(-2 * globelMargin);
        make.height.mas_equalTo(3 * globelMargin);
    }];
    
    [self.addressTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.reserveTimeTitleLabel.mas_bottom).offset(2 * globelMargin);
        make.left.equalTo(weakSelf.reserveTimeTitleLabel);
    }];
    
    [self.addressTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.addressTitleLabel.mas_bottom).offset(2 * globelMargin);
        make.left.equalTo(weakSelf.containerView).offset(2 * globelMargin);
        make.right.equalTo(weakSelf.containerView).offset(-2 * globelMargin);
        make.height.mas_equalTo(8 * globelMargin);
    }];
    
    [self.requiredInfoTitleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.addressTextView.mas_bottom).offset(2 * globelMargin);
        make.left.right.equalTo(weakSelf.containerView);
        make.height.mas_equalTo(5 * globelMargin);
    }];
    
    [self.requiredInfoTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.requiredInfoTitleView.mas_bottom).offset(2 * globelMargin);
        make.left.equalTo(weakSelf.containerView).offset(2 * globelMargin);
        make.right.equalTo(weakSelf.containerView).offset(-2 * globelMargin);
        make.height.mas_equalTo(10 * globelMargin);
    }];
    
    [self.noticeTitleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.requiredInfoTextView.mas_bottom).offset(4 * globelMargin);
        make.left.right.equalTo(weakSelf.containerView);
        make.height.mas_equalTo(5 * globelMargin);
    }];
    
    [self.noticeTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.noticeTitleView.mas_bottom).offset(2 * globelMargin);
        make.left.equalTo(weakSelf.containerView).offset(2 * globelMargin);
        make.right.equalTo(weakSelf.containerView).offset(-2 * globelMargin);
        make.height.mas_equalTo(16 * globelMargin);
    }];
    
    [self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.containerView);
        make.top.equalTo(weakSelf.noticeTextView.mas_bottom).offset(5 * globelMargin);
        make.size.mas_equalTo(buttonSize);
        make.bottom.equalTo(weakSelf.containerView).offset(-4 * globelMargin);
    }];
    
    [self setModel];
}

@end
